package edu.towson.cis.cosc442.project3.vendingmachine;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class VendingMachineTest {
	VendingMachine testMachine;
	
	@Before
	public void SetUp(){
		testMachine = new VendingMachine();
		testMachine.addItem(new VendingMachineItem("Carrots",1.25), "A");
		testMachine.addItem(new VendingMachineItem("Cheese", 1), "B");
		testMachine.addItem(new VendingMachineItem("Take 5",2.05),"C");
	}
    /*
     * testing to see if it is possible to create new vending machine
     */
	@Test
	public void testVendingMachine() {
		testMachine = new VendingMachine();
		VendingMachine testMachine = new VendingMachine();
	}

	@Test
	public void testAddItem() {
		/*testMachine.addItem(new VendingMachineItem("Carrots",1.25), "A");
		testMachine.addItem(new VendingMachineItem("Cheese", 1), "B");
		testMachine.addItem(new VendingMachineItem("Take 5",2.05),"C");*/
		testMachine.addItem(new VendingMachineItem("Panda Jerky", 1.50),"D");
		assertTrue(testMachine.getItem("D").equals(new VendingMachineItem("Panda Jerky", 1.50)));
		//assertTrue(testMachine.getItem("E")==null);
		
	}
	/*
	 * this test is designed to test the getItem exception, when the user illegally calls for an item
	 */
	@Test(expected = VendingMachineException.class)
	public void testGetItemException() throws VendingMachineException {
	    testMachine.getItem("E");
	    //testMachine.addItem(new VendingMachineItem("baboon",3), "A");
	}
	
	/*
	 * this test is designed to catch an exception from addItem Where the 
	 * user adds an illegal item
	 */
	@Test(expected = VendingMachineException.class)
	public void testAddItemException() throws VendingMachineException {
	    testMachine.addItem(new VendingMachineItem("baboon",3), "A");
	}
	
	/*
	 * This test the removeItem exception being thrown when a user removes 
	 * something that isn't in the vending machine
	 */
	@Test(expected = VendingMachineException.class)
	public void testRemoveItemException() throws VendingMachineException {
	    testMachine.removeItem("D");
	}
	/*
	 * this test tests the InsertMoney exception
	 */
	@Test(expected = VendingMachineException.class)
	public void testInsertMoneyException() throws VendingMachineException {
	    testMachine.insertMoney(-5);
	}
	/*
	 * this test tests the default exception
	 */
	@Test(expected = VendingMachineException.class)
	public void testDefaultException() throws VendingMachineException {
	    throw new VendingMachineException();
	}

	/*
	 * This test is designed to check if getItem is actually retrieving the Vending machine item
	 * that it is supposed to retrieve.  It also tests to see how to code handles retrieving bad inputs.
	 */
	@Test
	public void testGetItemA() {
		VendingMachineItem testItem = new VendingMachineItem("Carrots", 1.25);
		assertTrue(testMachine.getItem("A").equals(testItem));
		testItem = new VendingMachineItem("Cheese", 1.0);
	}
	public void testGetItemB() {
		VendingMachineItem testItem2 = new VendingMachineItem("Cheese", 1.0);
		assertTrue(testMachine.getItem("B").equals(testItem2));
	}
	public void testGetItemC() {
		VendingMachineItem testItem3 = new VendingMachineItem("Take 5", 2.05);
		assertTrue(testMachine.getItem("C").equals(testItem3));
	}
	public void testGetItemD() {
		assertTrue(testMachine.getItem("D")==null);
		
	}
	/*
	 * testRemoveItem tests the program to insure that the correct item is being removed
	 */
	@Test
	public void testRemoveItem() {
		testMachine.removeItem("A");
		assertTrue(testMachine.getItem("A")==null);
		assertTrue(testMachine.getItem("B")!=null);
		assertTrue(testMachine.getItem("C")!=null);
		
	}

	/*
	 * testInsertMoney is tested by first having 5 added to the balance and then seeing if the balance comes out as expected);
	 */
	@SuppressWarnings("deprecation")
	@Test
	public void testInsertMoney() {
		testMachine.insertMoney(5);
		assertTrue(5==testMachine.getBalance());
	}

	/*
	 * testGetBalance tests both initial value of the balance and the balance after money is inserted
	 */
	@SuppressWarnings("deprecation")
	@Test
	public void testGetBalance() {
		assertTrue(0== testMachine.getBalance());
		testMachine.insertMoney(5);
		assertTrue(5 == testMachine.getBalance());
	}
/*
 * testing make purchase to ensure that purchases go through all the motions
 */
	@Test
	public void testMakePurchase() {
		testMachine.insertMoney(5);
		testMachine.makePurchase("A");
		assertTrue(3.75== testMachine.getBalance());
		assertTrue(testMachine.getItem("A")==null);
	}
/*
 * testing return change to insure that correct change is returned after purchase and before purchase
 */
	@Test
	public void testReturnChange() {
		testMachine.insertMoney(2.75);
		assertTrue(testMachine.returnChange()==2.75);
		testMachine.insertMoney(3);
		testMachine.makePurchase("A");
		assertTrue(testMachine.returnChange()==1.75);
	}

	
	
	@After
	public void tearDown(){
		testMachine = null;
	}
}
